package com.rh.bmw4s.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rh.bmw4s.R;
import com.rh.bmw4s.bean.FilterBean;
import com.rh.bmw4s.utills.ScreenUtils;
import com.rh.bmw4s.utills.SizeUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2017/9/28.
 */

public class FilterAdapter extends ListBaseAdapter<FilterBean> {
    private int itemWidth = 0;
    Context context;
    //    单个参数
    HashMap<Integer, String> maps = new HashMap();
    //    拼接参数
    HashMap<Integer, String> maps1 = new HashMap();
    //
    HashMap<String, String> paraMaps = new HashMap();

    public FilterAdapter(Context context) {
        super(context);
        this.context = context;
        paraMaps.clear();
        //动态获取item宽度
        int width = ScreenUtils.getScreenWidth();
        itemWidth = (int) (width - SizeUtils.dp2px(60f)) / 3;
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_filter;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, int position) {
        final FilterBean bean = getDataList().get(position);
        TextView tvTitle = holder.getView(R.id.tv_title);
        LinearLayout llCustom = holder.getView(R.id.ll_custom);
        final EditText etNum1 = holder.getView(R.id.et_num_1);
        final EditText etNum2 = holder.getView(R.id.et_num_2);
        final TagFlowLayout flowLayout = holder.getView(R.id.flowlayout);
        final String title = bean.getTitle();
        if (title.contains("价格") || title.contains("里程") || title.contains("年限")) {
            llCustom.setVisibility(View.VISIBLE);
            etNum1.setText(bean.getNum1());
            etNum2.setText(bean.getNum2());
        } else {
            llCustom.setVisibility(View.GONE);
        }
        if (title.contains("颜色")) {
            flowLayout.setMaxSelectCount(-1);
        } else {
            flowLayout.setMaxSelectCount(1);
        }
        tvTitle.setText(bean.getTitle());

        final List<FilterBean.SortBean> sortBeans = bean.getSort();
        TagAdapter<FilterBean.SortBean> tagAdapter = new TagAdapter<FilterBean.SortBean>(bean.getSort()) {
            @Override
            public View getView(FlowLayout parent, int position, FilterBean.SortBean sortBean) {
                String tip = sortBean.getName();
                TextView textView = (TextView) LayoutInflater.from(context)
                        .inflate(R.layout.tv, flowLayout, false);
                textView.setText(tip);
                textView.getLayoutParams().width = itemWidth;
                return textView;
            }

            @Override
            public void onSelected(int position, View view) {
                super.onSelected(position, view);
            }

            @Override
            public boolean setSelected(int position, FilterBean.SortBean sortBean) {
                return super.setSelected(position, sortBean);
            }
        };
        reset(tagAdapter);
        flowLayout.setAdapter(tagAdapter);
        flowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                if (title.contains("颜色")) {
//                    多选
                    String ids = "";
                    for (int i : selectPosSet) {
                        ids = ids + sortBeans.get(i).getId() + ",";
                    }
                    maps.put(bean.getCate_id(), ids);
                    paraMaps.put(bean.getTitle(), ids);
                } else {
                    for (int i : selectPosSet) {
                        paraMaps.put(bean.getTitle(), sortBeans.get(i).getId() + "");
                        maps.put(bean.getCate_id(), sortBeans.get(i).getId() + ",");
                    }
                }
                if (title.contains("价格") || title.contains("里程") || title.contains("年限")) {
                    etNum1.setText("");
                    etNum2.setText("");
                }
            }
        });
//        监听自定义区间，取值
        if (title.contains("价格") || title.contains("里程") || title.contains("年限")) {
            etNum1.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    String etNum2Str = etNum2.getText().toString().trim();
                    String etNum1Str = editable.toString().trim();
                    if (!TextUtils.isEmpty(etNum1Str) && !TextUtils.isEmpty(etNum2Str)) {
                        try {
                            int num1 = Integer.valueOf(etNum1Str);
                            int num2 = Integer.valueOf(etNum2Str);
                            if (num1 > num2) {
                                etNum2.setError("请从低到高填写价格");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if (TextUtils.isEmpty(etNum1Str) && TextUtils.isEmpty(etNum2Str)) {
                        if (maps1.containsKey(bean.getCate_id())) {
                            maps1.remove(bean.getCate_id());
                        }
                        if (maps1.containsKey(bean.getTitle())) {
                            paraMaps.remove(bean.getTitle());
                        }
                        return;
                    }
                    if (TextUtils.isEmpty(etNum1Str) || TextUtils.isEmpty(etNum2Str)) {
                        return;
                    }
                    bean.setNum1(etNum1Str);
                    bean.setNum2(etNum2Str);
                    paraMaps.put(bean.getTitle(), etNum1Str + "-" + etNum2Str);
                }
            });
            etNum2.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    String etNum1Str = etNum1.getText().toString().trim();
                    String etNum2Str = editable.toString().trim();
                    if (!TextUtils.isEmpty(etNum1Str) && !TextUtils.isEmpty(etNum2Str)) {
                        try {
                            int num1 = Integer.valueOf(etNum1Str);
                            int num2 = Integer.valueOf(etNum2Str);
                            if (num1 > num2) {
                                etNum2.setError("请从低到高填写价格");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if (TextUtils.isEmpty(etNum1Str) && TextUtils.isEmpty(etNum2Str)) {
                        if (maps1.containsKey(bean.getCate_id())) {
                            maps1.remove(bean.getCate_id());
                        }
                        if (maps1.containsKey(bean.getTitle())) {
                            paraMaps.remove(bean.getTitle());
                        }
                        return;
                    }
                    if (TextUtils.isEmpty(etNum1Str) || TextUtils.isEmpty(etNum2Str)) {
                        return;
                    }
                    bean.setNum1(etNum1Str);
                    bean.setNum2(etNum2Str);
                    paraMaps.put(bean.getTitle(), etNum1Str + "-" + etNum2Str);
                }
            });
        }
    }


    public HashMap<String, String> getParaMaps() {
        return paraMaps;
    }

    public void clearMaps() {
        paraMaps.clear();
    }

    public void reset(TagAdapter<FilterBean.SortBean> tagAdapter) {
        tagAdapter.setSelectedList(0);
    }
}
